"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const CryptoJS = require("crypto-js");
const errors_1 = require("./errors");
function getNonceStr() {
    var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var maxPos = $chars.length;
    var noceStr = "";
    for (let i = 0; i < 32; i++) {
        noceStr += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    // oldNonceStr = noceStr;
    return noceStr;
}
exports.getNonceStr = getNonceStr;
function getMD5Sign(key, params) {
    let names = Object.getOwnPropertyNames(params);
    names.sort((a, b) => a < b ? -1 : 1);
    let str = names.map(o => o + "=" + params[o]).join("&") + "&key=" + key;
    let r = CryptoJS.MD5(str).toString().toUpperCase();
    return r;
}
exports.getMD5Sign = getMD5Sign;
function getTimeStamp() {
    var timestamp = new Date().getTime();
    var timestampstring = timestamp.toString(); //一定要转换字符串
    return timestampstring;
}
function getPackage(partnerId, partnerKey, params) {
    console.assert(params["key"] == undefined);
    params["key"] = partnerKey;
    let names = Object.getOwnPropertyNames(params);
    names.sort((a, b) => a < b ? -1 : 1);
    let signString = names.map(o => `${o}=${params[o]}`).join("&");
    let md5SignValue = ("" + CryptoJS.MD5(signString)).toUpperCase();
    let completeString = names.map(o => `${o}=${encodeURIComponent(params[o])}`) + "&sign=" + md5SignValue;
    ;
    return completeString;
}
class ConfigReader {
    constructor(config) {
        this.config = config;
    }
    getIsSandBox() {
        if (this.config.isSandBox != null)
            return this.config.isSandBox;
        return false;
    }
    getAppId() {
        if (!this.config.appId)
            throw errors_1.errors.configFieldNull("appId");
        return this.config.appId;
    }
    getAppKey() {
        if (!this.config.appKey)
            throw errors_1.errors.configFieldNull("appKey");
        return this.config.appKey;
    }
    getParanerId() {
        if (!this.config.partnerId)
            throw errors_1.errors.configFieldNull("partnerId");
        return this.config.partnerId;
    }
    getParanerKey() {
        if (!this.config.partnerKey)
            throw errors_1.errors.configFieldNull("partnerKey");
        return this.config.partnerKey;
    }
    getMiniAppId() {
        if (!this.config.miniAppId)
            throw errors_1.errors.configFieldNull("miniAppId");
        return this.config.miniAppId;
    }
    getMiniAppKey() {
        if (!this.config.miniAppKey)
            throw errors_1.errors.configFieldNull("miniAppKey");
        return this.config.miniAppKey;
    }
}
exports.ConfigReader = ConfigReader;
